<?php

namespace app\api\controller\wash;

use app\common\controller\Api;
use app\common\model\Admin;
use app\common\model\factory\Category;
use app\common\model\User;
use app\common\model\wash\Goods as WashGoods;

/**
 * 清洗
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 分裂列表
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiSummary ()
     * @ApiBody ()
     * @ApiParams (name="shop_id", type="integer", required=true, sample="", description="门店ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function categorylist()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'shop_id'  => 'require|number',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $shop = User::where('id', $this->request->post('shop_id'))->find();
        
        $res = Category::where('admin_id', Admin::where('id', $shop->company_id)->value('pid'))->where('status', 'normal')->order('sort desc')->select();
        $this->responseMsg(0, $res);
    }

    /**
     * 商品列表
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiSummary ()
     * @ApiBody ()
     * @ApiParams (name="category_id", type="integer", required=true, sample="", description="分类ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function list()
    {
        // $q->last= Admin::region($q->admin->region_id);
        $res = WashGoods::with(['cate'])->where('category_id', $this->request->post('category_id'))->where('fa_wash_goods.status', '1')->select();
        foreach ($res as $k => $v) {
            $v->city_corporation= Admin::region($v->admin->region_id);
        }
        $this->responseMsg(0, $res);
    }

    /**
     * 商品详情
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiSummary ()
     * @ApiBody ()
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function detail()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|gt:0',
            ]
        );

        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        // $shop = User::where('id', $this->request->post('shop_id'))->find();
        $res =  WashGoods::with(['category'])->where('id', $this->request->post('id'))->where('status', '1')->find();
        if (!$res) {
            $this->responseMsg(1005, [], '数据未找到');
        }
        $this->responseMsg(0, $res);
    }
}
